package com.turing.usdtpay.mvp.fragment;

import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.turing.usdtpay.R;
import com.turing.usdtpay.base.BaseFragment;
import com.turing.usdtpay.entity.CommissionManagerData;
import com.turing.usdtpay.entity.PersonCommissionData;
import com.turing.usdtpay.entity.TeamCommissionData;
import com.turing.usdtpay.mvp.contract.CommissionManagerContract;
import com.turing.usdtpay.mvp.dialog.ManageDialog;
import com.turing.usdtpay.mvp.presenter.CommissionManagerPresenter;
import com.turing.usdtpay.utils.FormatUtils;
import com.turing.usdtpay.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @作者：JAY
 * @创建时间：2019/10/15 14:19
 * @描述：
 **/
public class CommissionManagerFragment extends BaseFragment implements CommissionManagerContract.View {
    private CommissionManagerContract.Present mPresent;
    private int mCurrentSelect = 0; //0.今日 1.累计
    private TextView mSelfProbability,mOneProbability,mTwoProbability,mSelfCNY,mOneCNY,mTwoCNY,mTeamCNY
            ,mSelfCount,mOneCount,mTwoCount,mSelfTrade,mOneTrade,mTwoTrade,mTeamReady;
    private LinearLayout mCommissionLayout;
    public static CommissionManagerFragment newInstance(Bundle bundle){
        CommissionManagerFragment fragment = new CommissionManagerFragment();
        if (fragment != null){
            fragment.setArguments(bundle);
        }
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setPresenter(new CommissionManagerPresenter(this));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_commission_manager, container, false);
        initView(view);
        initTitle();
        initData();
        return view;
    }

    private void initData() {
//        Bundle bundle = getArguments();
//        CommissionManagerData data = (CommissionManagerData) bundle.getSerializable("data");
//        if (data != null){
//            List<PersonCommissionData> com_info = data.getCom_info();
//            TeamCommissionData team_com_info = data.getTeam_com_info();
//            initData(com_info,team_com_info);
//        }
        mPresent.checkData(getActivity(),0);
    }

    private void initView(View view) {
        mCommissionLayout = (LinearLayout) view.findViewById(R.id.commission_layout);
        mSelfProbability = (TextView) view.findViewById(R.id.self_probability);
        mSelfCNY = (TextView) view.findViewById(R.id.self_cny);
        mSelfCount = (TextView) view.findViewById(R.id.self_count);
        mSelfTrade = (TextView) view.findViewById(R.id.self_trade);
        mOneProbability = (TextView) view.findViewById(R.id.one_probability);
        mOneCNY = (TextView) view.findViewById(R.id.one_cny);
        mOneCount = (TextView) view.findViewById(R.id.one_count);
        mOneTrade = (TextView) view.findViewById(R.id.one_trade);
        mTwoProbability = (TextView) view.findViewById(R.id.two_probability);
        mTwoCNY = (TextView) view.findViewById(R.id.two_cny);
        mTwoCount = (TextView) view.findViewById(R.id.two_count);
        mTwoTrade = (TextView) view.findViewById(R.id.two_trade);
        mTeamCNY = (TextView) view.findViewById(R.id.team_cny);
        mTeamReady = (TextView) view.findViewById(R.id.team_ready);
        mCommissionLayout.setOnClickListener(this);
    }

    private void initTitle() {
        setTitle("佣金管理");
        showBackBtn(true);
        setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back();
            }
        });
        showTitleLogo(true);
        setOnClickTitleLogo(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final ManageDialog dialog = new ManageDialog(getActivity());
                dialog.setOnClickListener(new ManageDialog.OnClickListener() {
                    @Override
                    public void onSelection(int type) {
                        mCurrentSelect = type;
                        mPresent.checkData(getActivity(),type);
                    }
                });
                dialog.show();
                dialog.setType(mCurrentSelect);
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == mCommissionLayout.getId()){
            openNewFragment(CommissionFragment.newInstance(null));
        }
    }

    private void initData(List<PersonCommissionData> personCommissionData, TeamCommissionData team) {

        for (int i=0;i < personCommissionData.size();i++){
            PersonCommissionData person = personCommissionData.get(i);
            int subType = person.getSub_type();
            if (subType == 1){
                updatePersonData(person,mSelfProbability,mSelfCNY,mSelfCount,mSelfTrade);
            }else if (subType == 2){
                updatePersonData(person,mOneProbability,mOneCNY,mOneCount,mOneTrade);
            }else if (subType == 3){
                updatePersonData(person,mTwoProbability,mTwoCNY,mTwoCount,mTwoTrade);
            }
        }
            updateTeamData(team,mTeamCNY,mTeamReady);
    }

    private void updatePersonData(PersonCommissionData data,TextView probability,TextView cny,TextView count,TextView trade){
        double p = (double) data.getGet_com() / 100;
        double c = (double) data.getNum() / 100;
        double m = (double)data.getMoney() / 100;
        probability.setText(FormatUtils.NoumberFromat2(p)+"%");
        cny.setText(FormatUtils.NoumberFromat2(c));
        count.setText(data.getOrder_num()+"");
        trade.setText(FormatUtils.NoumberFromat2(m));
    }

    private void updateTeamData(TeamCommissionData data,TextView cny,TextView ready){
        double c = (double) data.getNum() / 100;
        double w = (double) data.getWait_num() / 100;
        cny.setText(FormatUtils.NoumberFromat2(c));
        ready.setText(FormatUtils.NoumberFromat2(w));
    }

    @Override
    public void onSuccess(CommissionManagerData data, int type) {
        List<PersonCommissionData> com_info = data.getCom_info();
        TeamCommissionData team_com_info = data.getTeam_com_info();
        initData(com_info,team_com_info);
    }

    @Override
    public void onFail(String msg) {
        ToastUtils.showNormolToast(getActivity(),msg);
    }

    @Override
    public void setPresenter(CommissionManagerContract.Present presenter) {
        mPresent = presenter;
    }
}
